package fylder.recycler.demo.view;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

/**
 * 测试,已不用
 *
 * Created by fylder on 15-11-23.
 */
public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {

    //默认第一页
    private int current_page = 1;

    private LinearLayoutManager layoutManager;

    public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) {
        this.layoutManager = linearLayoutManager;
    }


    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        Log.w("123", "newState:" + newState);
        int visibleItemCount = layoutManager.getChildCount();
        int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
        int totalItemCount = layoutManager.getItemCount();
        Log.w("123", "lastVisibleItem:" + lastVisibleItem + "\ttotalItemCount:" + totalItemCount);
        if (visibleItemCount > 0 && newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem >= totalItemCount - 1) {
            if (totalItemCount > 1) {
                Log.w("123", "scroll loading");
                current_page++;
                onLoadMore(current_page);
            }
        }
    }

    public abstract void onLoadMore(int current_page);
}